import redis

# 创建 Redis 连接对象
r = redis.Redis(host='localhost', port=6379, db=0)

# 设置哈希字段
r.hset('user:1', 'name', 'Alice')
r.hset('user:1', 'age', 25)

# 获取哈希字段的值
name = r.hget('user:1', 'name')
print(name.decode('utf-8'))

# 获取整个哈希
user = r.hgetall('user:1')
for key, value in user.items():
    print(f"{key.decode('utf-8')}: {value.decode('utf-8')}")